﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Bll;
using System.Data;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.Web.Customer
{
    public partial class FavoriteProduct : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(Page_Load);
            AspNetPager1.PageChanged += new EventHandler(AspNetPager1_PageChanged);

            gvProductList.RowDeleting += new GridViewDeleteEventHandler(gvProductList_RowDeleting);
            gvProductList.RowCommand += new GridViewCommandEventHandler(gvProductList_RowCommand);
        }

        void gvProductList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "addto")
            {
                Guid ProductID = new Guid(e.CommandArgument.ToString());

                string strURL = "~/Customer/ProductDetail.aspx?ProductID=" + ProductID.ToString();
                Response.Redirect(strURL);


                //BizShopCartEntity entity = new BizShopCartEntity();
                //entity.ProductID = ProductID;
                //entity.CustomerID = base.User.Identity.LoginUser.UserID;
                //entity.Quantity = 0;
                //entity.CreatedAt = DateTime.Now;
                //entity.Deleted = false;
                //BizShopCartBll bll = new BizShopCartBll();
                //bll.AddProductToShopcart(entity);

                //ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('添加成功.');", true);
            }
        }

        void gvProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid ID = new Guid(gvProductList.DataKeys[e.RowIndex]["ID"].ToString());
            CustomerBll bll = new CustomerBll();
            bll.Delete<BizCustomerFavoriteEntity>(" and ID = '"+ID+"'");

            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }

        private void BindGrid(int PageIndex)
        {
            CustomerBll bll = new CustomerBll();
            int RecordCount = 0;
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null;
            parm = new SqlDbParameter();
            parm.ColumnName = "[CustomerID]";
            parm.ColumnType = DbType.Guid;
            parm.ParameterName = "@CustomerID";
            parm.ParameterValue = base.User.Identity.LoginUser.UserID;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            DataTable dt = bll.GetCustomerFavorite(parms, " CreatedAt Desc", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvProductList.DataSource = dt;
            gvProductList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

    }
}